package com.hgk.wanwan.main.fragment.persentfragment.viewUI;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;

import width.hgk.wanwan.R;

public class GalleryActivity extends Activity {

    private ImageView ivTopImage;
    private Gallery mGallery;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gallery);
        initView();
    }

    private void initView() {
        ivTopImage = (ImageView) findViewById(R.id.iv_my_imageview);
        mGallery = (Gallery) findViewById(R.id.gallery_my_bottom);
        mGallery.setAdapter(new ImageAdapter(this));
        mGallery.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(GalleryActivity.this,""+position,Toast.LENGTH_SHORT).show();
                ivTopImage.setImageResource(view.getId());
            }
        });
    }

    public class ImageAdapter extends BaseAdapter{

         int mGalleryItemBackground;//Item装饰背景
        private Context mContext;//上下文对象
        private Integer[] pics = {
                R.drawable.pic_two,R.drawable.pic_three,R.drawable.pic_six,R.drawable.pic_one,R.drawable.pic_four,
                R.drawable.pic_five};
        public ImageAdapter(Context context) {
            mContext = context;
            //读取styleable资源
            TypedArray a = obtainStyledAttributes(R.styleable.HelloGallery);
            mGalleryItemBackground = a.getResourceId(R.styleable.HelloGallery_android_galleryItemBackground,0);
            a.recycle();
        }

        @Override
        public int getCount() {
            return pics.length;
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView iv = new ImageView(mContext);
            iv.setId(pics[position]);
            iv.setLayoutParams(new Gallery.LayoutParams(120, 120));
            iv.setScaleType(ImageView.ScaleType.FIT_XY);
            iv.setBackgroundResource(pics[position]);
            return iv;
        }

    }
    public static void launch(Context context){
        context.startActivity(new Intent(context,GalleryActivity.class));
    }
}
